﻿using System;
using System.Diagnostics;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace XUCrypto
{
    namespace Editor
    {
        internal class XUCryptoWindow : EditorWindow
        {
            [MenuItem("XUPK/XUCrypto/Tests", false, 1)]
            public static void SetConfig()
            {
                EditorWindow window = GetWindow<XUCryptoWindow>("Crypto");
                window.minSize = window.maxSize = new Vector2(812, 600);
                window.Show();
            }

            private string filePath;

            private TimeSpan spanRC4Encrypt;
            private TimeSpan spanRC4Decrypt;
            private TimeSpan spanBase64Encrypt;
            private TimeSpan spanBase64Decrypt;

            private long sizeFile;
            private long sizeRC4;
            private long sizeBase64;

            private void OnGUI()
            {
                GUILayout.Space(10);

                filePath = GUILayout.TextField(filePath);

                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                if (GUILayout.Button("选择文件"))
                {
                    filePath = EditorUtility.OpenFilePanel("文件", Application.dataPath, "*.*");
                    sizeFile = new FileInfo(filePath).Length;
                }
                GUILayout.EndHorizontal();

                GUILayout.Space(10);

                GUILayout.BeginHorizontal();
                {
                    GUILayout.BeginVertical("box", GUILayout.Width(400));
                    {
                        if (GUILayout.Button("RC4测试"))
                        {
                            if (!string.IsNullOrEmpty(filePath))
                            {
                                string pathEncrypt = filePath + ".rc4.encrypt";
                                string pathDecrypt = filePath + ".rc4.decrypt";
                                {
                                    byte[] bytes = File.ReadAllBytes(filePath);
                                    Stopwatch sw = new Stopwatch();
                                    sw.Start();
                                    bytes = XUCryptoRC4.Encrypt(bytes, "rc4.encrypt");
                                    sw.Stop();
                                    spanRC4Encrypt = sw.Elapsed;
                                    File.WriteAllBytes(pathEncrypt, bytes);
                                }
                                {
                                    byte[] bytes = File.ReadAllBytes(pathEncrypt);
                                    Stopwatch sw = new Stopwatch();
                                    sw.Start();
                                    bytes = XUCryptoRC4.Decrypt(bytes, "rc4.encrypt");
                                    sw.Stop();
                                    spanRC4Decrypt = sw.Elapsed;
                                    File.WriteAllBytes(pathDecrypt, bytes);
                                }
                                sizeRC4 = new FileInfo(pathEncrypt).Length;

                                AssetDatabase.Refresh();
                            }
                        }
                        if (sizeFile > 0 && sizeRC4 > 0)
                        {
                            GUILayout.Label("源文件大小：" + GetSizeString(sizeFile));
                            GUILayout.Label("加密后大小：" + GetSizeString(sizeRC4));
                            GUILayout.Label("加密比例：" + Mathf.FloorToInt(sizeRC4 * 1f / sizeFile * 100) + "%");
                            GUILayout.Label("加密时间：" + spanRC4Encrypt.TotalMilliseconds + "ms");
                            GUILayout.Label("解密时间：" + spanRC4Decrypt.TotalMilliseconds + "ms");
                        }
                    }
                    GUILayout.EndVertical();

                    GUILayout.BeginVertical("box", GUILayout.Width(400));
                    {
                        if (GUILayout.Button("Base64测试"))
                        {
                            if (!string.IsNullOrEmpty(filePath))
                            {
                                string pathEncrypt = filePath + ".base64.encrypt";
                                string pathDecrypt = filePath + ".base64.decrypt";
                                {
                                    byte[] bytes = File.ReadAllBytes(filePath);
                                    Stopwatch sw = new Stopwatch();
                                    sw.Start();
                                    string content = XUCryptoBase64.Encrypt(bytes);
                                    sw.Stop();
                                    spanBase64Encrypt = sw.Elapsed;
                                    File.WriteAllText(pathEncrypt, content);
                                }
                                {
                                    string content = File.ReadAllText(pathEncrypt);
                                    Stopwatch sw = new Stopwatch();
                                    sw.Start();
                                    byte[] bytes = XUCryptoBase64.Decrypt(content);
                                    sw.Stop();
                                    spanBase64Decrypt = sw.Elapsed;
                                    File.WriteAllBytes(pathDecrypt, bytes);
                                }
                                sizeBase64 = new FileInfo(pathEncrypt).Length;

                                AssetDatabase.Refresh();
                            }
                        }
                        if (sizeFile > 0 && sizeBase64 > 0)
                        {
                            GUILayout.Label("源文件大小：" + GetSizeString(sizeFile));
                            GUILayout.Label("加密后大小：" + GetSizeString(sizeBase64));
                            GUILayout.Label("加密比例：" + Mathf.FloorToInt(sizeBase64 * 1f / sizeFile * 100) + "%");
                            GUILayout.Label("加密时间：" + spanBase64Encrypt.TotalMilliseconds + "ms");
                            GUILayout.Label("解密时间：" + spanBase64Decrypt.TotalMilliseconds + "ms");
                        }
                    }
                    GUILayout.EndVertical();
                }
                GUILayout.EndHorizontal();
            }

            public static string GetSizeString(long fileSize)
            {
                if (fileSize >= (1024 * 1024))
                {
                    return string.Format("{0:F2}MB", fileSize * 1f / (1024 * 1024));
                }
                else
                {
                    return string.Format("{0:F2}KB", fileSize * 1f / 1024);
                }
            }
        }
    }
}